package com.example.demo.util;

import java.util.ArrayList;
import java.util.List;

import com.example.demo.entity.EtRole;
import com.example.demo.system.TreeModel;



/**
 * 
 * 数据处理工具类
 * 
 */
public class DataHandle {

	// 功能的树形数据
	/*public static List<TreeModel> HandleFuncTreeData(List<> list) {

		List<TreeModel> funcs = new ArrayList<TreeModel>();

		for (HsManageFunc func : list) {
			if ("0".equals(func.getParentId()) || func.getParentId() == null
					|| "".equals(func.getParentId())) {
				TreeModel model = new TreeModel();
				model.setId(func.getId());

				if (func.getIsActivated() == 0) {
					model.setIcon("../img/cancel-on.png");
				}

				model.setText(func.getFuncName());
				model.state = model.new state();
				model.state.setOpened(true);
				funcs.add(model);
			}
		}

		funcs = FuncRecursion(funcs, list);

		return funcs;
	}

	public static List<TreeModel> FuncRecursion(List<TreeModel> funcs, List<HsManageFunc> list) {
		if (funcs.size() <= 0) {
			return funcs;
		}
		for (TreeModel f : funcs) {

			List<TreeModel> children = new ArrayList<TreeModel>();
			for (HsManageFunc func1 : list) {
				if (!"0".equals(func1.getParentId()) && func1.getParentId() != null) {
					if (f.getId().equals(func1.getParentId())) {
						TreeModel model = new TreeModel();
						model.setId(func1.getId());
						if (func1.getIsActivated() == 0) {
							model.setIcon("../img/cancel-on.png");
						}
						model.setText(func1.getFuncName());
						model.state = model.new state();
						model.state.setOpened(true);

						children.add(model);
					}
				}
			}
			f.setChildren(children);
			FuncRecursion(children, list);

		}
		return funcs;
	}*/

	
	// 角色的树形数据
	public static List<TreeModel> HandleRoleTreeData(List<EtRole> list) {

		List<TreeModel> funcs = new ArrayList<TreeModel>();

		for (EtRole func : list) {
//			if ("0".equals(func.getParentRoleId()) || func.getParentRoleId() == null
//					|| "".equals(func.getParentRoleId())) {
				TreeModel model = new TreeModel();
				model.setId(func.getId());
				model.setText(func.getRoleName());
				model.state = model.new state();
				model.state.setOpened(false);
				funcs.add(model);
//			}
		}

		funcs = RoleRecursion(funcs, list);

		return funcs;
	}

	public static List<TreeModel> RoleRecursion(List<TreeModel> funcs, List<EtRole> list) {
		if (funcs.size() <= 0) {
			return funcs;
		}
		for (TreeModel f : funcs) {

			List<TreeModel> children = new ArrayList<TreeModel>();
//			for (HsManageRole func1 : list) {
//				if (!"0".equals(func1.get) && func1.getParentRoleId() != null) {
//					if (f.getId().equals(func1.getParentRoleId())) {
//						TreeModel model = new TreeModel();
//						model.setId(func1.getRoleId());
//						if (func1.getIsActivated() == 0) {
//							model.setIcon("../img/cancel-on.png");
//						}
//						model.setText(func1.getRoleName());
//						model.state = model.new state();
//						model.state.setOpened(false);
//
//						children.add(model);
//					}
//				}
//			}
			f.setChildren(children);
			RoleRecursion(children, list);

		}
		return funcs;
	}
	

}
